package com.jiang.daily.Q2962;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/04/30 9:43
 */
public class Solution {
    public long countSubarrays(int[] nums, int k) {
        int max = 0;
        long res = 0;
        int cnt = 0;
        for (int x: nums) {
            max = Math.max(max, x);
        }
        for (int i = 0, j = 0; j < nums.length; j++) {
            if (nums[j] == max) {
                cnt++;
            }
            while (cnt == k) {
                res += nums.length - j;
                if (nums[i++] == max) {
                    cnt--;
                }
            }

        }
        return res;
    }
}
